package com.supermap.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.supermap.commontypes.PropertiesInfo;

public class PropertiesHelper {

    public static Properties loadProperties(String path) throws IOException {
        FileInputStream fis = null;
        Reader reader = null;
        try {
            fis = new FileInputStream(path);
            reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            Properties result = new Properties();
            result.load(reader);
            return result;
        } finally {
            fis.close();
            reader.close();
        }
    }

    public static void checkExtractPropertiesInfo(PropertiesInfo info) throws Exception {
        if (StringUtils.isBlank(info.getSrcDir())) {
            throw new Exception("srcDir的值不能为空！");
        }
        if (StringUtils.isBlank(info.getSampleOutputFile())) {
            throw new Exception("sampleOutPutFile的值不能为空！");
        }
        if (StringUtils.isBlank(info.getSampleInfoFile())) {
            throw new Exception("sampleInfoFile的值不能为空！");
        }
        if (info.getPatterns() == null || info.getPatterns().length == 0) {
            throw new Exception("patterns的值不能为空！");
        }
        java.util.List<String> patterns = Arrays.asList(info.getPatterns());
        for (int i = 0; i < patterns.size(); i++) {
            if (StringUtils.isBlank(patterns.get(i))) {
                patterns.remove(i);
            }
        }
        if (patterns.isEmpty()) {
            throw new Exception("patterns的值不能为空！");
        } else {
            info.setPatterns(patterns.toArray(new String[patterns.size()]));
        }
    }

    public static void checkUpdatePropertiesInfo(PropertiesInfo info) throws Exception {
        if (StringUtils.isBlank(info.getSrcDir())) {
            throw new Exception("srcDir的值不能为空！");
        }
        if (StringUtils.isBlank(info.getSampleFile())) {
            throw new Exception("sampleFile的值不能为空！");
        }
        if (StringUtils.isBlank(info.getDestDir())) {
            throw new Exception("destDir的值不能为空！");
        }
    }
}
